<?php
/**
	 * 后台管理Action
	 * 处理后台管理业务
	 * 
	 */
class AdminAction extends Action {
	
	// function index() {
	// $this->display();
	// }
	function login() {
		if ($_SESSION ['admin_type'] != 1) {
			$this->assign ( 'jumpUrl', U ( 'Index/mapsDemo' ) );
			$this->error ( '您不具有该权限' );
		} else {
			$this->assign ( 'jumpUrl', U ( 'Admin/index' ) );
			$this->success ( '成功进入' );
		}
	}
	
	/**
	 * 线路相关方法
	 */
	function lines() {
		$lines = D ( 'Lines' );
		$res = $lines->getLinesList ( '', true );
		$this->assign ( "lines", $res );
		$this->display ();
	}
	function editLine() {
		$_GET ['line_id'] = intval ( $_GET ['line_id'] );
		if ($_GET ['line_id'] < 0)
			$this->error ( '参数错误' );
		$map ['line_id'] = $_GET ['line_id'];
		$lines = M ( "lines" )->where ( $map )->select ();
		
		$this->assign ( "inspector_id", $lines [0] ['inspector_id'] );
		$inspector = M ( "inspector" )->field ( "inspector_id,name" )->select ();
		$this->assign ( "inspector", $inspector );
		$this->assign ( "lines", $lines );
		$this->assign ( 'type', 'edit' );
		$this->display ();
	}
	function doEditLine() {
		$where ['line_id'] = $_POST ['line_id'];
		$data ['name'] = $_POST ['line_name'];
		$data ['inspector_id'] = $_POST ['inspector_id'];
		$data ['node_amount'] = $_POST ['node_amount'];
		$res = M ( "lines" )->where ( $where )->data ( $data )->save ();
		if ($res) {
			$this->assign ( 'jumpUrl', U ( 'Admin/lines' ) );
			$this->success ( '保存成功' );
		} else {
			$this->assign ( 'jumpUrl', U ( 'Admin/lines' ) );
			$this->error ( '内容为改动！' );
		}
	}
	/**
	 * 增加路线
	 */
	function addLine() {
		// echo "addLine";
		$this->assign ( 'type', 'add' );
		$inspector = M ( "inspector" )->field ( "name,inspector_id" )->select ();
		$this->assign ( 'inspector', $inspector );
		$this->display ( "editLine" );
	}
	function doAddLine() {
		$map ['name'] = $_POST ['line_name'];
		
		$map ['inspector_id'] = $_POST ['inspector_id'];
		
		$map ['node_amount'] = 0;
		// var_dump($map);
		$res = M ( "lines" )->add ( $map );
		if ($res) {
			$this->assign ( 'jumpUrl', U ( 'Admin/lines' ) );
			$this->success ( '添加成功' );
		} else {
			$this->assign ( 'jumpUrl', U ( 'Admin/lines' ) );
			$this->error ( '添加失败' );
		}
	}
	function delectLine() {
		$map ['line_id'] = $_POST ['line_id'];
		$res = M ( 'lines' )->where ( $map )->delete ();
		$res3 = M ( 'node' )->where ( $map )->select ();
		if ($res3) {
			$res2 = M ( 'node' )->where ( $map )->delete ();
		} else {
			$res2 = 1;
		}
		
		if ($res && $res2) {
			echo 1;
		} else {
			echo 0;
			return;
		}
	}
	
	/**
	 * 线路节点相关方法
	 */
	function node() {
		$map ['line_id'] = $_GET ['line_id'];
		$lineName = M ( "lines" )->where ( $map )->field ( "name" )->select ();
		$model = M ( "node" );
		$nodes = $model->where ( $map )->select ();
		$this->assign ( 'nodes', $nodes );
		$this->assign ( 'line_id', $_GET ['line_id'] );
		$this->assign ( "lineName", $lineName [0] ['name'] );
		$this->display ();
	}
	function addNode() {
		$lines = D ( 'Lines' );
		$res = $lines->getLinesList ( '', true );
		$line = array ();
		foreach ( $res as $k => $v ) {
			$line [$k] ['line_id'] = $v ['line_id'];
			$line [$k] ['line_name'] = $v ['name'];
		}
		$this->assign ( 'line', $line );
		$this->assign ( 'line_id', $_GET ['line_id'] );
		
		$this->assign ( 'type', 'add' );
		$this->display ( "editNode" );
	}
	function doAddNode() {
		$nodeName = $_POST ['nodeName'];
		$longitude = $_POST ['longitude'];
		$latitude = $_POST ['latitude'];
		$lineID = $_POST ['lineID'];
		$order_id = $_POST ['order_id'];
		$nowTime = getdate ( time () );
		
		if (! ($longitude && $latitude && $lineID && $order_id)) {
			$this->assign ( 'jumpUrl', U ( 'Admin/node', array (
					'line_id' => $lineID 
			) ) );
			$this->error ( '节点信息不完整！' );
		}
		
		$map ['name'] = $nodeName;
		$map ['line_id'] = $lineID;
		$map ['longitude'] = $longitude;
		$map ['latitude'] = $latitude;
		$map ['order_id'] = $order_id;
		$Model = M ( "node" );
		$result = $Model->add ( $map );
		$map2 ['line_id'] = $lineID;
		$res = M ( "lines" )->where ( $map2 )->field ( "node_amount" )->select ();
		$data ['node_amount'] = $res [0] ['node_amount'] + 1;
		$res = M ( "lines" )->where ( $map2 )->data ( $data )->save ();
		
		if (! ($result && $res)) {
			$this->assign ( 'jumpUrl', U ( 'Admin/node', array (
					'line_id' => $lineID 
			) ) );
			$this->error ();
		} else {
			$this->assign ( 'jumpUrl', U ( 'Admin/node', array (
					'line_id' => $lineID 
			) ) );
			$this->success ();
		}
	}
	function delectNode() {
		
		// 删除节点
		$map ['node_id'] = $_POST ['node_id'];
		
		// 将该节点所在的线路的结点个数减1
		$res2 = M ( "node" )->where ( $map )->field ( "line_id" )->select ();
		// var_dump($res2);
		
		$map2 ['line_id'] = $res2 [0] ['line_id'];
		$res3 = M ( "lines" )->where ( $map2 )->field ( "node_amount" )->select ();
		// var_dump($res3);
		
		$data ['node_amount'] = $res3 [0] ['node_amount'] - 1;
		$res4 = M ( "lines" )->where ( $map2 )->data ( $data )->save ();
		
		$res = M ( 'node' )->where ( $map )->delete ();
		// var_dump($res);
		// var_dump($res4);
		// die();
		
		if ($res && $res4) {
			echo 1;
		} else {
			echo 0;
			return;
		}
	}
	function editNode() {
		$_GET ['node_id'] = intval ( $_GET ['node_id'] );
		// var_dump($_GET['node_id']);
		
		if ($_GET ['node_id'] < 0) {
			$this->error ( '参数错误' );
		}
		$map ['node_id'] = $_GET ['node_id'];
		$node = M ( "node" )->where ( $map )->select ();
		
		$this->assign ( "line_id", $node [0] ['line_id'] );
		$this->assign ( "node", $node );
		
		$lines = D ( 'Lines' );
		$res = $lines->getLinesList ( '', true );
		$line = array ();
		foreach ( $res as $k => $v ) {
			$line [$k] ['line_id'] = $v ['line_id'];
			$line [$k] ['line_name'] = $v ['name'];
		}
		// var_dump($line);
		$this->assign ( "line", $line );
		$this->assign ( 'type', 'edit' );
		$this->display ();
	}
}
